#include "Tkgles.h"
#include "graphic.h"

EXPORT GLvoid Pixel(Color * color[3], GLint x, GLint y, GLint triang[3][3], GLfloat depth[3]){

	GLfloat a = 0.5f*((triang[0][0]-x)*(triang[1][1]-triang[0][1]) - (triang[0][0] - triang[1][0])*(y-triang[0][1]));
	GLfloat b = 0.5f*((triang[1][0]-x)*(triang[2][1]-triang[1][1]) - (triang[1][0] - triang[2][0])*(y-triang[1][1]));
	GLfloat c = 0.5f*((triang[2][0]-x)*(triang[0][1]-triang[2][1]) - (triang[2][0] - triang[0][0])*(y-triang[2][1]));
	GLfloat all = 0.5f*((triang[0][0]-triang[2][0])*(triang[1][1]-triang[0][1]) - (triang[0][0] - triang[1][0])*(triang[2][1]-triang[0][1]));
	a = a/all;
	b = b/all;
	c = c/all;

	a = a > 0? a:-a;
	b = b > 0? b:-b;
	c = c > 0? c:-c;
	//printf ("%f, %f, %f\n",a,b,c);

        //Get depth
        GLfloat this_depth = a*depth[0] + b*depth[1] + c*depth[2];

        if(IsVisible(x,y,this_depth)){
            GLfloat red = a*color[2]->red + b*color[1]->red + c*color[0]->red;
            GLfloat green = a*color[2]->green + b*color[1]->green + c*color[0]->green;
            GLfloat blue = a*color[2]->blue + b*color[1]->blue + c*color[0]->blue;

            GLint ired = (GLint)(red*255);
            GLint igreen = (GLint)(green*255);
            GLint iblue = (GLint)(blue*255);
            //printf("%X, %X, %X\n",ired,igreen,iblue);

            GLint this_color = (ired<<16)|(igreen<<8)|iblue;
            DrawPixel(x,-y,this_color);
            UpdateZBuffer(x,y,this_depth);
        }
}

EXPORT Color * get_color(GLint index){
	Context * context = getContext();
	Color * out;

	GLvoid * pt;
	if(!context)
		return NULL;
	pt = context->ColorArray.pointer;
	out = Kmalloc(sizeof(Color));
	if(context->ColorArray.type == GL_FLOAT){
		if(context->ColorArray.stride == 0){
			pt = pt+index*sizeof(GLfloat)*4;
		}else{
			pt = pt+index*(sizeof(GLbyte)*context->ColorArray.stride);
		}
		out->red = (GLfloat)(*(GLfloat*)pt);
		out->green = (GLfloat)(*(GLfloat*)(pt+sizeof(GLfloat)));
		out->blue = (GLfloat)(*(GLfloat*)(pt+2*sizeof(GLfloat)));
		out->alpha = (GLfloat)(*(GLfloat*)(pt+3*sizeof(GLfloat)));
        //printf("%f,%f,%f\n",out->red,out->green,out->blue);
	}
	return out;
}

EXPORT void MakeSpan(GLint a, GLint b, GLint y, GLint triang[3][3],Color * vertex_color[3], GLfloat depth[3]){
	Context * context = getContext();
	GLint temp,x;
    //printf("Making span, from %d, to %d, line %d\n", a,b,y);
/*
	if (color1 && color2 && color3)
		printf("Getting color successfully\n");

printf("%f, %f, %f\n",color1->red,color1->green,color1->blue);
printf("%f, %f, %f\n",color2->red,color2->green,color2->blue);
printf("%f, %f, %f\n",color3->red,color3->green,color3->blue);
*/
	//Trip off any section of span that outside the viewport
	if ( a > b){
		temp = a;
		a = b;
		b = temp;
	}
	temp = (GLint)context->ViewPort.ox + context->ViewPort.px;
	b = b > temp?temp:b;
	temp = (GLint)context->ViewPort.ox - context->ViewPort.px;
	a = a < temp?temp:a;

	for ( x = a; x <=b; x++){
		Pixel(vertex_color,x,y,triang,depth);
	}
}

